package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.strategy.ItemTypeStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * Package: com.atguigu.tingshu.user.strategy.impl
 *
 * @author xiongkunYang
 * @description: 处理购买项目类型 ItemType 为专辑的情况
 * @since 2025/6/26 10:11
 */
@Slf4j
@Component(SystemConstant.ORDER_ITEM_TYPE_ALBUM)
public class AlbumStrategyImpl implements ItemTypeStrategy {

    private final UserPaidAlbumMapper userPaidAlbumMapper;

    public AlbumStrategyImpl(UserPaidAlbumMapper userPaidAlbumMapper) {
        this.userPaidAlbumMapper = userPaidAlbumMapper;
    }

    @Override
    public void savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        log.info("处理购买项目类型为：专辑");
        String orderNo = userPaidRecordVo.getOrderNo();
        Long userId = userPaidRecordVo.getUserId();
        // 查询专辑购买记录
        Long albumId = userPaidRecordVo.getItemIdList().get(0);
        Long count = this.userPaidAlbumMapper.selectCount(new LambdaQueryWrapper<UserPaidAlbum>()
                .eq(UserPaidAlbum::getUserId, userId)
                .eq(UserPaidAlbum::getAlbumId, albumId));
        if (count > 0) {
            log.error("用户:{} 已购买过专辑:{} ! ", userId, albumId);
            return;
        }
        // 创建用户专辑购买记录对象
        UserPaidAlbum userPaidAlbum = new UserPaidAlbum();
        userPaidAlbum.setOrderNo(orderNo);
        userPaidAlbum.setUserId(userId);
        userPaidAlbum.setAlbumId(albumId);
        this.userPaidAlbumMapper.insert(userPaidAlbum);

    }
}